package com.cloudminds.resttest.utils;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * Utils - Spring
 *
 * @author cc
 */
@Component
@Lazy(false)
public final class SpringContextUtils implements ApplicationContextAware, DisposableBean {
    /**
     * applicationContext
     */
    private static ApplicationContext applicationContext;

    private SpringContextUtils() {
    }

    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextUtils.applicationContext = applicationContext;
    }

    public void destroy() throws Exception {
        applicationContext = null;
    }

    /**
     * 获取applicationContext
     *
     * @return applicationContext
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 获取实例
     *
     * @param name Bean名称
     * @return 实例
     */
    public static Object getBean(String name) {
        Assert.hasText(name, "bean name should be specified");
        assertContextInjected();
        return applicationContext.getBean(name);
    }

    /**
     * 获取实例
     *
     * @param name Bean名称
     * @param type Bean类型
     * @return 实例
     */
    public static <T> T getBean(String name, Class<T> type) {
        Assert.hasText(name, "bean name should be specified");
        Assert.notNull(type, "bean type should be specified");
        assertContextInjected();
        return applicationContext.getBean(name, type);
    }

    public static <T> T getBean(Class<T> type) {
        Assert.notNull(type, "bean type should be specified");
        assertContextInjected();
        return applicationContext.getBean(type);
    }
//	/**
//	 * 获取国际化消息
//	 *
//	 * @param code
//	 *            代码
//	 * @param args
//	 *            参数
//	 * @return 国际化消息
//	 */
//	public static String getMessage(String code, Object... args) {
//        LocaleResolver localeResolver = null;
//        Locale locale = Locale.SIMPLIFIED_CHINESE;
//        try {
//            localeResolver = getBean(LocaleResolver.class);
//			if (localeResolver instanceof FixedLocaleResolver) {
//                locale = localeResolver.resolveLocale(null);
//            } else {
//                locale = LocaleContextHolder.getLocale();
//            }
//            String rule = applicationContext.getEnvironment().getProperty("spring.local.rule");
//            if (StringUtils.isNotBlank(rule)) {
//                String[] rules = StringUtils.split(rule, ",");
//                String language = locale.toLanguageTag().replace("-", "_");
//                Assert.isTrue(ArrayUtils.contains(rules, language), "not support " + language);
//            }
//        } catch (Exception ex) {
//
//        }
//        return applicationContext.getMessage(code, args, locale);
//	}

    /**
     * 检查ApplicationContext不为空.
     */
    private static void assertContextInjected() {
        Assert.notNull(applicationContext, "applicationContext属性未注入.");
    }
}